package es.udc.fic.hamNeggs.pojo;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.JoinTable;
import javax.persistence.ManyToMany;
import javax.persistence.OneToMany;
import javax.persistence.Table;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

@Entity
@Table(name = "hamneggs.users")
public class User {
    @Id
    @GeneratedValue
    @Column(name = "usr_id")
    private Long             id;

    @Column(name = "usr_name")
    private String           name;

    @Column(name = "usr_fullName")
    private String           fullName;

    @Column(name = "usr_pass")
    private String           password;

    @OneToMany(mappedBy = "meetingLeader")
    private Set<Meeting>     meetings;

    @OneToMany(mappedBy = "user")
    private Set<Works>       works;

    @OneToMany(mappedBy = "user")
    private Set<UserStories> userStories;

    @ManyToMany(cascade = { CascadeType.PERSIST })
    @JoinTable(name = "hamneggs.goesto",
            joinColumns = { @JoinColumn(name = "usr_id") },
            inverseJoinColumns = { @JoinColumn(name = "mtg_id") })
    private Set<Meeting>     goesToMeetings;

    public User() {

    }

    public String getName() {
        return name;
    }

    public void setName(final String name) {
        this.name = name;
    }

    public String getFullName() {
        return fullName;
    }

    public void setFullName(final String fullName) {
        this.fullName = fullName;
    }

    public Long getId() {
        return id;
    }

    public Set<Meeting> getMeetings() {
        return meetings;
    }

    public List<Works> getWorks() {
        List<Works> lworks = new ArrayList<Works>();
        lworks.addAll(works);
        Collections.sort(lworks,
                new Comparator<Works>() {
                    public int compare(Works o1, Works o2) {
                        return o1.getProject().getId().intValue() - o2.getProject().getId().intValue();
                    }
                });
        return lworks;
    }

    public List<Meeting> getGoesToMeetings() {
        List<Meeting> lmeetings = new ArrayList<Meeting>();
        lmeetings.addAll(goesToMeetings);
        Collections.sort(lmeetings,
                new Comparator<Meeting>() {
                    public int compare(Meeting o1, Meeting o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        return lmeetings;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(final String password) {
        this.password = password;
    }

    public void setOneLeaderMeeting(Meeting mtg) {
        if (meetings == null)
            meetings = new HashSet<Meeting>();
        meetings.add(mtg);

    }

    public void setOneGoesMeeting(Meeting mtg) {
        if (goesToMeetings == null)
            goesToMeetings = new HashSet<Meeting>();
        goesToMeetings.add(mtg);

    }

    public void setOneWorks(Works wrk) {
        if (works == null)
            works = new HashSet<Works>();
        works.add(wrk);

    }

    public void setId(Long id) {
        this.id = id;
    }

    public void setMeetings(Set<Meeting> meetings) {
        this.meetings = meetings;
    }

    public void setWorks(Set<Works> works) {
        this.works = works;
    }

    public void setGoesToMeetings(Set<Meeting> goesToMeetings) {
        this.goesToMeetings = goesToMeetings;
    }

    public List<UserStories> getUserStories() {
        List<UserStories> luserStories = new ArrayList<UserStories>();
        luserStories.addAll(userStories);
        Collections.sort(luserStories,
                new Comparator<UserStories>() {
                    public int compare(UserStories o1, UserStories o2) {
                        return o1.getId().intValue() - o2.getId().intValue();
                    }
                });
        ;
        return luserStories;
    }

    public void setUserStories(Set<UserStories> userStories) {
        this.userStories = userStories;
    }

    public void setOneUserStories(UserStories ust) {
        if (userStories == null)
            userStories = new HashSet<UserStories>();
        userStories.add(ust);

    }

}
